package com.boboPlanet.user.mq.consumer;

import com.boboPlanet.comm.context.LoginUserContext;
import com.boboPlanet.comm.entity.po.LoginUserInfoPO;
import com.boboPlanet.comm.rabbitmq.RabbitMqRoutingConfig;
import com.boboPlanet.user.mapper.UserOperationMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: TODO 接收消息
 * @Author: mis_wu
 * @Date: 2023/2/18
 **/
@Slf4j
@Component
public class ReceiveXxlUserMsg {
    @Resource
    private UserOperationMapper userOperationMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @RabbitListener(queues = RabbitMqRoutingConfig.Xxl_Send_User_State_Queue )
    @Transactional(rollbackFor = Exception.class)
    public void process(LoginUserInfoPO user) {
        log.info("receive msg way1==> xxl-job接收到发送用户异常状态消息...");
        log.info("user data: {}",user);

        userOperationMapper.clearUserLoginState(user.getAccountId());

        stringRedisTemplate.delete(user.getLoginToken());

        LoginUserContext.clear();

    }


//    @RabbitListener(queues = RabbitMqConfig.Xxl_Send_User_State_Queue )
    public void messageHandler(@NotNull Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag){

        log.info("receive msg way2==> xxl-job接收到发送用户异常状态消息...");
        byte[] user = message.getBody();
        log.info("user data: {}",user);

    }
}
